package com.example.demo.controller;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import com.example.demo.dto.PageExtra;
import com.example.demo.dto.RoutingRuleDTO;
import com.example.demo.request.CreateRoutingRuleRequest;
import com.example.demo.response.ApiResponse;
import com.example.demo.service.RoutingRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/routing-rules")

public class RoutingRuleController {

        @Autowired
        private RoutingRuleService routingRuleService;


        @GetMapping
        public ResponseEntity<ApiResponse<List<RoutingRuleDTO>>> getRoutingRules(
                         @RequestParam(defaultValue = "0") int page,
                         @RequestParam(defaultValue = "10") int size,
                        HttpServletRequest request) {

                List<RoutingRuleDTO> routingRules = routingRuleService.getRoutingRules(page, size);
                long total = routingRuleService.countRoutingRules();
                PageExtra extra = new PageExtra(page, size, total);

                return ResponseEntity.ok(
                                ApiResponse.success(routingRules, "请求成功", request.getRequestURI(), extra));
        }


        @GetMapping("/{id}")
        public ResponseEntity<ApiResponse<RoutingRuleDTO>> getRoutingRule(
                         @PathVariable Integer id,
                        HttpServletRequest request) {

                RoutingRuleDTO routingRule = routingRuleService.getRoutingRuleById(id);
                return ResponseEntity.ok(
                                ApiResponse.success(routingRule, "查询成功", request.getRequestURI(), null));
        }


        @PostMapping
        public ResponseEntity<ApiResponse<RoutingRuleDTO>> createRoutingRule(
                        @Valid @RequestBody CreateRoutingRuleRequest request,
                        HttpServletRequest requestObj) {

                com.example.demo.entity.RoutingRule routingRule = routingRuleService.createRoutingRule(request);
                RoutingRuleDTO dto = RoutingRuleDTO.fromEntity(routingRule);
                return ResponseEntity.status(201).body(
                                ApiResponse.created(dto, "路由规则创建成功", requestObj.getRequestURI()));
        }


        @PutMapping("/{id}")
        public ResponseEntity<ApiResponse<RoutingRuleDTO>> updateRoutingRule(
                        @PathVariable Integer id,
                        @Valid @RequestBody CreateRoutingRuleRequest request,
                        HttpServletRequest requestObj) {

                routingRuleService.updateRoutingRule(id, request);
                RoutingRuleDTO routingRule = routingRuleService.getRoutingRuleById(id);
                return ResponseEntity.ok(
                                ApiResponse.success(routingRule, "路由规则更新成功", requestObj.getRequestURI(), null));
        }


    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteRoutingRule(
                    @PathVariable Integer id,
                    HttpServletRequest requestObj) {

                routingRuleService.deleteRoutingRule(id);
                return ResponseEntity.ok(
                                ApiResponse.success(null, "路由规则删除成功", requestObj.getRequestURI(), null));
        }
        
    @GetMapping("/search/by-role-name")
    public ResponseEntity<List<RoutingRuleDTO>> searchByRoleName(@RequestParam String roleName) {
        List<RoutingRuleDTO> routingRules = routingRuleService.searchByRoleName(roleName);
        return ResponseEntity.ok(routingRules);
    }
    
    @GetMapping("/search/by-dept-name")
    public ResponseEntity<List<RoutingRuleDTO>> searchByDeptName(@RequestParam String deptName) {
        List<RoutingRuleDTO> routingRules = routingRuleService.searchByDeptName(deptName);
        return ResponseEntity.ok(routingRules);
    }
}